package views

import (
	"errors"
	"haiqiu-blog-go/common"
	"haiqiu-blog-go/service"
	"net/http"
	"strconv"
	"strings"
)

func (*HTMLApi) Detail(w http.ResponseWriter, r *http.Request) {
	detail := common.Template.Detail
	//	获取参数路径
	path := r.URL.Path
	pidStr := strings.TrimPrefix(path, "/p/")
	if pidStr == "" {
		detail.WriteError(w, errors.New("不识别该请求"))
	}
	pidStr = strings.TrimSuffix(pidStr, ".html")
	pid, err := strconv.Atoi(pidStr)
	if err != nil {
		detail.WriteError(w, errors.New("解析文章ID出错"))
	}
	postDetail, err := service.GetPostDetail(pid)
	if err != nil {
		detail.WriteError(w, err)
	}
	detail.WriteData(w, postDetail)
}

func (*HTMLApi) Writing(w http.ResponseWriter, r *http.Request) {
	writing := common.Template.Writing
	wr := service.Writing()
	writing.WriteData(w, wr)
}

func (*HTMLApi) Pigeonhole(w http.ResponseWriter, r *http.Request) {
	pigeonhole := common.Template.Pigeonhole
	pigeonholeRes := service.FindPostPigeonhole()
	pigeonhole.WriteData(w, pigeonholeRes)
}

func (*HTMLApi) Custom(w http.ResponseWriter, r *http.Request) {
	custom := common.Template.Custom
	custom.WriteData(w, nil)
}
